# -*- coding: utf-8 -*-
import os, sys
BASE_DIR = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'Spider'))

BOT_NAME = 'Spider'
SPIDER_MODULES = ['Spider.spiders']
NEWSPIDER_MODULE = 'Spider.spiders'
# 不遵循网站ROBOTS规定
ROBOTSTXT_OBEY = False
# 不保存cookie,适用不需要登录验证的网页
COOKIES_ENABLED = False
#################################################################全局优化配置
#启用AutoThrottle调试(debug)模式，展示每个接收到的response,【【【【【【【生产环境下需要关闭】】】】】】
AUTOTHROTTLE_DEBUG = True
#启用AutoThrottle扩展,自动优化scrapy下载
AUTOTHROTTLE_ENABLED = True
#初始化下载延迟N秒
AUTOTHROTTLE_START_DELAY = 2
#在高延迟情况下最大的下载延迟60秒
AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
#Scrapy downloader并发请求的最大值,默认16
CONCURRENT_REQUESTS = 32
#下载器在下载同一个网站下一个页面前需要等待的时间(秒)。可以用来限制爬取速度,减轻服务器压力
DOWNLOAD_DELAY = 1
#对单个网站进行并发请求的最大值
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#对单个IP进行并发请求的最大值
#CONCURRENT_REQUESTS_PER_IP = 16
# Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'Scrapy_Redis_Test.middlewares.ScrapyRedisTestSpiderMiddleware': 543,
#}
#################################################################DOWNLOADER_MIDDLEWARES下载器
#fake-useragent插件随机获取user-agent的方式
#USER_AGENT = 'Spider (+http://www.yourdomain.com)'
RANDOM_USER_AGENT_TYPE = "random"
#item中存储图片的字段名称
IMAGES_URLS_FIELD = "image_urls"
FILES_URLS_FIELD = "file_urls"
#存储路径
IMAGES_STORE  = "#"
FILES_STORE = "#"
#120天内抓取的文件都不会被重抓
FILES_EXPIRES = 120
IMAGES_EXPIRES = 120
#生成图片缩略图
# IMAGES_THUMBS = {
#     'small': (50, 50),
#     'big': (400, 400),
# }
DOWNLOADER_MIDDLEWARES = {
    #取消默认的user-agent机制
    'scrapy.downloadermiddlewares.useragent.UserAgentDownloadMiddleware': None,
    #自定义获取user-agent
    'Spider.middlewares.RandomUserAgentDownloadMiddleware': 1,
    #自定义获取代理ip
    'Spider.middlewares.RandomIpProxyDownloadMiddleware': 2,
    #通过DOWNLOADER_MIDDLEWARES拦截下载器下载,改用selenium内置的chrome浏览器动态加载页面并返回
    'Spider.middlewares.SeleniumChromeDownloadMiddleware': 3,
}
##################################################################（重写scrapy_redis的https协议下载器）
# DOWNLOAD_HANDLERS = {
#     'https': 'Spider.handler.mydownloader.MyDownloadHandler'
# }
#################################################################SPIDER_MIDDLEWARES拦截器
#SPIDER_MIDDLEWARES = {
#    'Spider.middlewares.TaobaospiderSpiderMiddleware': 543,
#}
#################################################################自定义pipelins
ITEM_PIPELINES = {
    #保存下载后的本地图片路径到item
    'Spider.pipelines.CustomImagesPipeline': 1,
    #导入数据到json文件中
    # 'Spider.pipelines.JsonExporterPipleline': 2,
    # 'Spider.pipelines.JsonLinesExporterPipleline': 2,
    # 导入数据到mysql
    # 'Spider.pipelines.AsyInsterMysqlPipeline': 2,
    #导入数据到mogondb
    'Spider.pipelines.InsertMongodbPipeline': 2,
    #保存数据到elasticsearch
    # 'Spider.pipelines.ElasticsearchPipeline': 2,
    # 导入数据到csv
    # 'Spider.pipelines.CSVExporterPipleline': 2,
    # 'Spider.pipelines.Excel2003ExporterPipleline': 2,
    # 此扩展将会序列化Item对象并保存到redis
    # 'scrapy_redis.pipelines.RedisPipeline': 100,
}
################################################################scrapy-redis配置
#默认情况下,RFPDupeFilter只记录第一次重复的请求。True将会使其记录所有重复的requests
DUPEFILTER_DEBUG = False
#重写scrapy中的scheduler调度器
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
#Scheduler中进行调度,负责对spider yield的requst请求进行去重
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
FEED_EXPORT_ENCODING = "utf-8"
###############################################################PYTHON日志配置
LOG_ENABLED = True
LOG_ENCODING = "UTF-8"
LOG_LEVEL = "INFO"
#所有的进程的标准输出和错误消息将被重定向到日志中
LOG_SHORT_NAMES = True
LOG_FORMAT = "%(asctime)s   %(levelname)s   %(message)s"
# LOG_DATEFORMAT = ""
LOG_FILE = ""
#################################################################redis连接参数
REDIS_URL = "redis://:li1234redis@127.0.0.1:6379/0"
REDIS_PASSWORD = 'li1234redis'
REDIS_HOST = '127.0.0.1'
REDIS_PORT = 6379
REDIS_DB = 0
#################################################################mongodb连接参数
MONGODB_URL = 'mongodb://admin_user:%li1234md@127.0.0.1:27017'
MONGODB_DB = 'admin'
MONGODB_COLLECTION = "taobao"
MONGODB_USER = 'admin_user'
MONGODB_PASSWORD = 'li1234md'
MONGODB_PORT = '27017'
MONGODB_HOST = '127.0.0.1'
############################################################################mysql配置
MYSQL_HOST = "127.0.0.1"
MYSQL_PORT = 3306
MYSQL_DBNAME = "taobao"
MYSQL_USER = "lixing"
MYSQL_PASSWORD = "12345"
MYSQL_CHARSET = "utf8"



# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False
# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}
# Enable or disable extensions
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}
# Enable and configure HTTP caching (disabled by default)
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
